package org.authsum.stitches.wicket.contentversion;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.authsum.stitches.external.*;
import org.authsum.stitches.services.*;
import org.authsum.stitches.wicket.BaseLandingPanel;
import org.authsum.stitches.wicket.BasePanel;
import org.authsum.stitches.wicket.SearchParms;
import org.authsum.stitches.wicket.contentdef.ContentDefSelectionListener;
import org.authsum.stitches.wicket.contentdef.ContentDefsPanel;
import  org.authsum.stitches.wicket.contentdef.detail.*;
import org.authsum.stitches.wicket.contentdef.table.ContentDefTable;
import org.authsum.stitches.wicket.contentversion.detail.*;
import org.authsum.stitches.wicket.contentversion.table.*;


import org.authsum.stitches.wicket.contentversion.detail.*;

public class ContentVersionsPanel  extends BaseLandingPanel implements
ContentVersionSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ContentVersionSelectionListener contentVersionSelectionListener = null;

	// public abstract ContentDefSelectionListener
	// getContentDefSelectionListener();

	public ContentVersionsPanel(String id,
			ContentVersionSelectionListener contentVersionSelectionListener) {
		super(id);
		this.contentVersionSelectionListener = contentVersionSelectionListener;
		ContentVersionHolder cvh = new ContentVersionHolder();

		detailPanel = new ContentVersionPanel("detailPanel", cvh, this);// ,PackageAttractionModal);

		postInit();

	}

	public ContentVersionsPanel(String id) {
		super(id);
		ContentVersionHolder cvh = new ContentVersionHolder();
		detailPanel = new ContentVersionPanel("detailPanel", cvh, this);// ,PackageAttractionModal);

		contentVersionSelectionListener = this;
		postInit();

	}

	public void contentVersionSelected(AjaxRequestTarget target,
			ContentVersionHolder contentVersionHolder) {
		editContentVersion(target, contentVersionHolder);

	}

	private void editContentVersion(ContentVersionHolder contentVersionHolder) {
		System.out.println("editContentDef2");
		detailPanel = new ContentVersionPanel("detailPanel", contentVersionHolder, this);
		editDetailObject();
	}

	protected void initTable(List list) {
		resultsTable = new ContentVersionTable("resultsTable", list,
				contentVersionSelectionListener);
		updateContainer.addOrReplace(resultsTable);
	}

	private static Log logger = LogFactory.getLog(ContentDefsPanel.class);

	public void editContentVersion(AjaxRequestTarget target, ContentVersionHolder cdf) {
		// TODO Auto-generated method stub
		// ImageContent imageContent = image.getImageContent();
		// image has been selected
		System.out.println("editContentDef");
		System.out.println("Editing cdf " + cdf.getName());
		try {
			// need to get the freshing imagecontent, the one that was passed in
			// is from search results
			ContentVersionRequest cdrequest = new ContentVersionRequest();
			cdrequest.setContentVersionId(cdf.getId());
			ContentVersionResponse cvresponse = getStitchesService()
					.findContentVersionById(cdrequest);
			editContentVersion(cvresponse.getContentVersionHolders()
					.getContentVersionHolder().get(0));
			// backToSearchResults.setVisible(true);
			target.addComponent(updateContainer);
		} catch (Exception e) {
			e.printStackTrace();
			error("An error occurred while trying to edit this content version.");
			error(e.getMessage());
			// target.addComponent(feedbackPanel);
		}

	}

	private void refreshTable(ContentVersionResponse contentVersionResponse) {
		initTable(contentVersionResponse.getContentVersionHolders()
				.getContentVersionHolder());

		backToSearchResults.setVisible(false);
		detailPanel.setVisible(false);
		centerDefaultPanel.setVisible(false);
	}

	protected void refreshTable(AjaxRequestTarget target,
			ContentVersionResponse contentVersionResponse) {

		refreshTable(contentVersionResponse);
		// DMIResult dmiResult = getDMIResult(dmiRequest);
		// backToSearchResults.setVisible(false);
		// detailPanel.setVisible(false);

		// initTable(dmiResult);

		target.addComponent(updateContainer);
		// target.addComponent(detailPanel);
		target.addComponent(backToSearchResults);
		target.addComponent(feedbackPanel);
		// target.addComponent(this);

	}

	@Override
	public void createDetailObject() {
		ContentVersionHolder cvh = new ContentVersionHolder();

		editContentVersion(cvh);

	}

	@Override
	public void performSearch(AjaxRequestTarget target, Form form) {
		SearchParms searchParms = (SearchParms) searchForm.getModel()
				.getObject();

		ContentVersionRequest contentVersionRequest = new ContentVersionRequest();
		contentVersionRequest.setKeywords(searchParms.getKeywords());

		ContentVersionResponse contentVersionResponse = getStitchesService()
				.findContentVersions(contentVersionRequest);

		// DMIResult dmiResult= getDMIResult(dmiRequest);
		refreshTable(target, contentVersionResponse);
		searchTips.setVisible(contentVersionResponse.getContentVersionHolders()
				.getContentVersionHolder().isEmpty());

	}


}
